// ExamGroupController.java
package com.quiz.bis.controller;

import com.quiz.bis.domain.bo.ExamGroupApplyBo;
import com.quiz.bis.domain.bo.ExamGroupBo;
import com.quiz.bis.domain.vo.ExamGroupApplicationVo;
import com.quiz.common.core.controller.BaseController;
import com.quiz.common.core.domain.R;
import com.quiz.common.mybatis.core.page.PageQuery;
import com.quiz.common.mybatis.core.page.TableDataInfo;
import com.quiz.bis.domain.bo.ExamGroupQueryBo;
import com.quiz.bis.domain.vo.ExamGroupVo;
import com.quiz.bis.service.IExamGroupService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 考试组管理控制器
 * @author ahuan
 */
@RestController
@RequestMapping("/exam/group")
@Tag(name = "考试组管理")
@RequiredArgsConstructor
public class ExamGroupController extends BaseController {
    private final IExamGroupService examGroupService;

    /**
     * 查询主要负责的考试组
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/mainOwner")
    public TableDataInfo<ExamGroupVo> mainOwnerGroups(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.queryMainOwnerGroups(queryBo,pageQuery);
    }

    /**
     * 查询作为次要成员的考试组
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/secondaryOwner")
    public TableDataInfo<ExamGroupVo> secondaryOwnerGroups(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.querySecondaryOwnerGroups(queryBo, pageQuery);
    }

    /**
     * 查询作为成员的考试组
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/member")
    @Operation(summary = "查询作为成员的考试组")
    public TableDataInfo<ExamGroupVo> memberGroups(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.queryMemberGroups(queryBo,pageQuery);
    }

    /**
     * 查询已禁用或退出的考试组
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/disabledOrQuit")
    @Operation(summary = "查询已禁用或退出的考试组")
    public TableDataInfo<ExamGroupVo> disabledOrQuitGroups(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.queryDisabledOrQuitGroups(queryBo,pageQuery);
    }

    /**
     * 查询待处理的邀请
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/pendingInvites")
    @Operation(summary = "查询待处理的邀请")
    public TableDataInfo<ExamGroupVo> pendingInvites(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.queryPendingInvites(queryBo,pageQuery);
    }

    /**
     * 申请加入考试组
     * @param inviteCode
     * @return
     */
    @PostMapping("/applyJoin")
    public R<Void> applyJoin(@RequestBody  ExamGroupApplyBo applyBo) {
        applyBo.setUserId(getUserId());
        return examGroupService.applyJoinGroup(applyBo);
    }

    /**
     * 处理邀请
     * @param inviteId
     * @param accept
     * @return
     */
    @PostMapping("/handleInvite/{inviteId}")
    @Operation(summary = "处理邀请")
    public R<Void> handleInvite(@PathVariable Long inviteId, @RequestParam boolean accept) {
        return examGroupService.handleInvite(inviteId, accept);
    }

    /**
     * 移交主要负责权限
     * @param groupId
     * @param newOwnerId
     * @return
     */
    @PostMapping("/transfer/{groupId}")
    @Operation(summary = "移交主要负责权限")
    public R<Void> transferOwnership(@PathVariable Long groupId, @RequestParam Long newOwnerId) {
        return examGroupService.transferOwnership(groupId, newOwnerId);
    }

    /**
     * 退出考试组
     * @param groupId
     * @return
     */
    @PostMapping("/quit/{groupId}")
    @Operation(summary = "退出考试组")
    public R<Void> quitGroup(@PathVariable Long groupId) {
        return examGroupService.quitGroup(groupId);
    }

    /**
     * 启用/禁用考试组
     * @param groupId
     * @param enable
     * @return
     */
    @PostMapping("/enable/{groupId}")
    @Operation(summary = "启用/禁用考试组")
    public R<Void> enableGroup(@PathVariable Long groupId, @RequestParam boolean enable) {
        return examGroupService.enableGroup(groupId, enable);
    }

    /**
     * 删除考试组
     * @param groupId
     * @return
     */
    @DeleteMapping("/{groupId}")
    @Operation(summary = "删除考试组")
    public R<Void> deleteGroup(@PathVariable Long groupId) {
        return examGroupService.deleteGroup(groupId);
    }


    /**
     * 新增考试组
     * @param bo
     * @return
     */
    @PostMapping
    @Operation(summary = "新增考试组")
    public R<Long> add(@Validated @RequestBody ExamGroupBo bo) {
        return examGroupService.addExamGroup(bo);
    }

    /**
     * 修改考试组
     * @param bo
     * @return
     */
    @PutMapping
    @Operation(summary = "修改考试组")
    public R<Void> update(@Validated @RequestBody ExamGroupBo bo) {
        return examGroupService.updateExamGroup(bo);
    }


    /**
     * 查询参加的考试组
     * @param queryBo
     * @param pageQuery
     * @return
     */
    @GetMapping("/student/list")
    public TableDataInfo<ExamGroupVo> listOfStudent(ExamGroupQueryBo queryBo, PageQuery pageQuery) {
        queryBo.setUserId(getUserId());
        return examGroupService.queryMemberGroupsOfStudent(queryBo, pageQuery);
    }

    /**
     * 根据邀请码搜索考试组
     * @param code
     * @return
     */
    @GetMapping("/searchByCode")
    public R<ExamGroupVo> searchByCode(@RequestParam String code) {
        return R.ok(examGroupService.searchGroupByCode(code));
    }

    /**
     * 申请加入考试组
     * @param applyBo
     * @return
     */
    @PostMapping("/student/apply")
    public R<Void> applyJoinGroup(@RequestBody ExamGroupApplyBo applyBo) {
        applyBo.setUserId(getUserId());
        return toR(examGroupService.studentJoinGroup(applyBo));
    }

    /**
     * 学生端退出考试组
     * @param groupId
     * @return
     */
    @PostMapping("/student/quit/{groupId}")
    @Operation(summary = "退出考试组")
    public R<Void> quitGroupOfStudent(@PathVariable Long groupId) {
        return examGroupService.quitGroupOfStudent(groupId);
    }

    @GetMapping("/student/applications")
    @Operation(summary = "获取学生申请记录")
    public TableDataInfo<ExamGroupApplicationVo> listApplications(PageQuery pageQuery) {
        return examGroupService.listStudentApplications(getUserId(), pageQuery);
    }

    /**
     * 取消申请(通过考试组id)
     * @param id
     * @return
     */
    @DeleteMapping("/student/applications/{id}")
    public R<Void> cancelApplication(@PathVariable Long id) {
        return toR(examGroupService.cancelApplication(getUserId(), id));
    }

    /**
     * 取消申请(通过成员记录Id)
     * @param id
     * @return
     */
    @DeleteMapping("/student/applications/by/member/{id}")
    public R<Void> cancelApplicationByRecordId(@PathVariable Long id) {
        return toR(examGroupService.cancelApplicationByRecordId(getUserId(), id));
    }







}